package cn.snoworld.controller;

import cn.snoworld.dto.CommonResult;
import cn.snoworld.entity.Comment;
import cn.snoworld.service.CommentService;
import cn.snoworld.vo.CommentVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("comment")
@Api(tags = "评论接口")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @GetMapping("{id}")
    @ApiOperation("根据ID获取评论")
    public CommonResult getCommentById(@PathVariable Integer id) {
        return CommonResult.OK(commentService.getById(id));
    }

    @GetMapping
    @ApiOperation("根据文章ID获取评论")
    public CommonResult getCommentByArticleId(
            @RequestParam Integer articleId,
            @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) {
        return CommonResult.OK(commentService.getCommentListByArticleId(articleId));
    }


    @PostMapping
    @ApiOperation("新增一条评论")
    public CommonResult addComment(@RequestBody CommentVo comment) {
        commentService.save(
                comment.getArticleId(),
                new Comment(
                        null,
                        comment.getParentId(),
                        comment.getUserId(),
                        comment.getText(),
                        null
                )
        );
        return CommonResult.OK();
    }

    @DeleteMapping("{id}")
    @ApiOperation("根据ID删除评论")
    public CommonResult removeCommentById(@PathVariable Integer id) {
        commentService.removeById(id);
        return CommonResult.OK();
    }
}
